package com.creative.accountsdk.holder;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.CheckClassInviteCodeProtocol;
import com.creative.accountsdk.net.account.CheckTeacherInviteCodeProtocol;
import com.creative.accountsdk.net.account.ChildAddClassProtocal;
import com.creative.accountsdk.net.account.TeacherInviteProtocol;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * Created by wang_qian on 2015/11/27.
 */
public class ChildAddClassHolder extends BaseHolder {
    private EditText edtTxtInviteCode;
    private EditText edtTxtRealName;
    private TextView tvClassName;
    private Button   btnAddClass;
    private TextView txtCheck;

    @Override
    protected void initViews() {

        edtTxtInviteCode = (EditText)viewRoot.findViewById(R.id.EdtTxtInviteCode);
        edtTxtRealName = (EditText)viewRoot.findViewById(R.id.EdtTxtRealName);
        tvClassName = (TextView)viewRoot.findViewById(R.id.tvSchoolName);
        btnAddClass = (Button)viewRoot.findViewById(R.id.btnAddClass);
        txtCheck = (TextView)viewRoot.findViewById(R.id.tvCheck);
        tvClassName = (TextView)viewRoot.findViewById(R.id.tvSchoolName);

        setListeners();

    }

    private void setListeners() {
        btnAddClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(edtTxtInviteCode.getText()) && !TextUtils.isEmpty(edtTxtRealName.getText())) {

                    addClass();
                }
            }
        });

        txtCheck.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                progress = DialogUtils.createProgress(activity);
                progress.show();
                final CheckClassInviteCodeProtocol protocol = new CheckClassInviteCodeProtocol(edtTxtInviteCode.getText().toString());

                protocol.debugUrl(DataCenter.getHttpClient());
                DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        progress.cancel();
                        if (protocol.parse(response, activity)) {
                            if (!TextUtils.isEmpty(protocol.className)) {
                                tvClassName.setVisibility(View.VISIBLE);
                                tvClassName.setText(activity.getResources().getString(R.string.school_name) + protocol.className);
                            }
                           // activity.finish();
                        }
                    }
                });
            }
        });
    }

    @Override
    public int getLayoutResID() {
        return R.layout.childaddclass_layout;
    }

    private void addClass() {
        progress = DialogUtils.createProgress(activity);
        progress.show();
        final ChildAddClassProtocal protocol = new ChildAddClassProtocal(edtTxtInviteCode.getText().toString(),
                edtTxtRealName.getText().toString());
        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, activity)) {
                    String classname = protocol.classObject.optString("name");
                /*    if (!TextUtils.isEmpty(classname)) {
                        showClassInfo(classname);
                    }*/
                    UserData.getInstance().initClassInfo(protocol.classObject);
                    UserData.getInstance().writeClassInfo(activity, protocol.classObject);
                    activity.finish();
                }
            }
        });
    }

    private void showClassInfo(String classname) {
        activity.hideHolder();
        activity.showHolder(new ChildClassInfoHolder(classname));
    }


}
